<?php
/**
 * Created by PhpStorm.
 * User: TianFeng
 * Date: 2017/11/14
 * Time: 16:02
 * 文件上传 阿里云
 */
namespace app\merchant\controller;
use \think\Log;
use app\common\lib\Uploader;
use think\File;

class Upload{

    public function uploadApi()
    {
        $filepath = input('filepath');
        $filetype = input('filetype','jpg');
        $Uploader = new Uploader();
        $fileName = 's' . "_" . time() . str_replace('.', '', str_replace(' ', '', microtime())).'.'.$filetype;
        $rt = $Uploader->uploadFromApi($filepath.'/'.$fileName);
        return $rt;
    }



    // 文件上传到本地
    public function uploadLocal(){
        $filename = $_FILES['file']['name'];
        $tmpname = $_FILES['file']['tmp_name'];
        $filename= time().'_'.$filename;
        if( move_uploaded_file($tmpname, ROOT_PATH . 'public' . DS . 'uploads/local/'.iconv("UTF-8", "GBK",$filename)))
        {
            return $filename;
        }else{
            return 1;
        }
    }
    //文件下载
    public function download()
    {
        $input=input('name');
        $input = iconv("utf-8","gb2312",$input);
        $file_array = explode("/",$input);
        $file_name = end($file_array);
        $file_dir = ROOT_PATH . 'public' . DS . 'uploads/image/';
        if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
         return '文件找不到';
        } else {
            $file = fopen($file_dir . $file_name,"r"); // 打开文件
// 输入文件标签
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: ".filesize($file_dir . $file_name));
            Header("Content-Disposition: attachment; filename=" . $file_name);
// 输出文件内容
            echo fread($file,filesize($file_dir . $file_name));
            fclose($file);
            exit;}
    }

     public function createtable($list,$filename,$header=array(),$index = array()){
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=".$filename.".xls");
        $teble_header = implode("\t",$header);
        $strexport = $teble_header."\r";
        foreach ($list as $row){
            foreach($index as $val){
                $strexport.=$row[$val]."\t";
            }
            $strexport.="\r";

        }
        $strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport);
        exit($strexport);
    }
    //生成文件


}